package com.ymm.web.controller;

import com.github.pagehelper.PageInfo;
import com.ymm.platform.api.constants.dto.request.ComplaintsInfoRequest;
import com.ymm.platform.api.constants.dto.response.ComplaintsInfoResponse;
import com.ymm.platform.api.constants.dto.response.ComplaintsStateResponse;
import com.ymm.platform.api.constants.dto.response.PendingComplaintsInfoResponse;
import com.ymm.platform.api.facade.ComplaintsService;
import com.ymm.platform.api.utils.Result;
import com.ymm.platform.api.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 申诉服务相关rest接口
 * @Author: Xuecheng Li
 * @CreateTime: 2022-08-14 21:03
 * @Version: 1.0.0
 */
@CrossOrigin
@RestController
@RequestMapping("/lulutong/platform/complaints")
@Slf4j
public class ComplaintsController {

    @Reference
    ComplaintsService complaintsService;

    @GetMapping("/queryComplaintsInfoByComplaintsId")
    Result<ComplaintsInfoResponse> queryComplaintsInfoByComplaintsId(@RequestParam("complaintsId") Long complaintsId) {

        if (complaintsId == null) {
            return Result.fail(ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.queryComplaintsInfoByComplaintsId(complaintsId);
    }

    /**
     * 用户提交申诉请求
     * @Param: [complaintsInfo]
     * @return: com.ymm.platform.api.utils.Result
     */
    @PostMapping("/commitComplaints")
    public Result commitComplaints(ComplaintsInfoRequest complaintsInfo) {
        log.info("申诉信息：{}", complaintsInfo);

        if (complaintsInfo == null) {
            return Result.fail(ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.commitComplaints(complaintsInfo);
    }
    
    /**
     * 用户查看历史申诉包含处理结果
     * @Param: [complainantId]
     * @return: com.ymm.platform.api.utils.Result<java.util.List<com.ymm.platform.api.constants.dto.response.ComplaintsInfoResponse>>
     */
    @GetMapping("/queryComplaintsInfo")
    public Result<List<ComplaintsInfoResponse>> queryComplaintsInfo(Long complainantId) {

        log.info("用户查看历史申诉包含处理结果，申诉人id：{}", complainantId);

        Result<List<ComplaintsInfoResponse>> listResult = complaintsService.queryComplaintsInfo(complainantId);
        return listResult;
    }
    
    
    /**
     * 用户取消指定申诉
     * @Param: [complaintsId]
     * @return: com.ymm.platform.api.utils.Result
     */
    @GetMapping("/cancelComplaints")
    public Result cancelComplaints(Long complaintsId) {

        log.info("用户取消指定申诉，用户id：{}", complaintsId);

        if (complaintsId == null) {
            return Result.fail(ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.cancelComplaints(complaintsId);
    }
    
    /**
     * 平台查询所有的待处理申诉
     * @Param: [pageNum]
     * @return: com.ymm.platform.api.utils.Result<java.util.List<com.ymm.platform.api.constants.dto.response.PendingComplaintsInfoResponse>>
     */
    @GetMapping("/queryPendingComplaints")
    public Result<PageInfo<PendingComplaintsInfoResponse>> queryPendingComplaints(@RequestParam("pageNum") Integer pageNum,
                                                                                  @RequestParam("pageSize") Integer pageSize) {

        log.info("平台查询所有的待处理申诉, pageNum:{}.pageSize:{}", pageNum, pageSize);

        if (pageNum == null) {
            return Result.fail(null, ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.queryPendingComplaints(pageNum, pageSize);
    }

    /**
     * 提交申诉处理结果
     * @Param: [complaintsId, handleDsc]
     * @return: com.ymm.platform.api.utils.Result
     */
    @PostMapping("/commitComplaintsHandleComment")
    public Result commitComplaintsHandleComment(@RequestParam(value = "complaintsId") Long complaintsId,
                                                @RequestParam(value = "handleDsc") String handleDsc) throws MQBrokerException, RemotingException, InterruptedException, MQClientException {

        log.info("提交申诉处理结果,申诉id：{}, handleDsc:{}", complaintsId, handleDsc);

        if (complaintsId == null || handleDsc == null) {
            return Result.fail(null, ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.commitComplaintsHandleComment(complaintsId, handleDsc);
    }

    @GetMapping("/queryComplaintsStateByOrderId")
    public Result<ComplaintsStateResponse> queryComplaintsStateByOrderId(@RequestParam("orderId") Long orderId) {
        log.info("查询指定订单申诉状态,orderId:{}", orderId);

        if (orderId == null) {
            return Result.fail(ResultCodeEnum.IN_PARAM_ERROR);
        }

        return complaintsService.queryComplaintsStateByOrderId(orderId);
    }
}
